#include<iostream>
#include<sw/redis++/redis++.h>
#include<vector>
#include<string>
#include<thread>
#include<chrono>
#include<unordered_map>
#include"Util.hpp"
using namespace std;
using namespace chrono_literals;

void test1(sw::redis::Redis& redis)
{
    cout << "lpush和lrange" << endl;
    redis.flushall();

    //插入单个元素
    redis.lpush("key1","111");
    //基于初始化列表
    redis.lpush("key1",{"222","333","444"});
    //基于迭代器区间
    vector<string> ret = {"555","666","777"};
    redis.lpush("key1",ret.begin(),ret.end());

    //使用lrange打印
    vector<string> result;
    redis.lrange("key1",0,-1,std::back_inserter(result)); //注意
    printContainer(result);
}

void test2(sw::redis::Redis& redis)
{
     cout << "rpush" << endl;
    redis.flushall();

    //插入单个元素
    redis.rpush("key1","111");
    //基于初始化列表
    redis.rpush("key1",{"222","333","444"});
    //基于迭代器区间
    vector<string> ret = {"555","666","777"};
    redis.rpush("key1",ret.begin(),ret.end());

    //使用lrange打印
    vector<string> result;
    redis.lrange("key1",0,-1,std::back_inserter(result)); //注意
    printContainer(result);
}

void test3(sw::redis::Redis& redis)
{
    cout << "lpop和rpop" << endl;
    redis.flushall();

    //构造一个list
    redis.rpush("key1",{"111","222","333","444"});

    //1
    auto val1 = redis.lpop("key1");
    if(val1) cout << "lpop num: " << val1.value() << endl;
    //4
    auto val2 = redis.rpop("key1");
    if(val2) cout << "lpop num: " << val2.value();

}

void test4(sw::redis::Redis& redis)
{
    cout << "blpop" << endl;
    redis.flushall();

    auto result = redis.blpop("key");
    if(result)
    {
        //重载了operator->
       cout << "key:" << result->first << endl;
       cout << "val:" << result->second << endl;
    }
}


void test5(sw::redis::Redis& redis)
{
    cout << "llen" << endl;
    redis.flushall();

    redis.lpush("key",{"111","222","333","444"});
    cout << "len key:" << redis.llen("key");
}

int main()
{
    sw::redis::Redis redis("tcp://127.0.0.1:6379");
    test5(redis);
    return 0;
}